package com.QK.cnstudy;

import java.util.ArrayList;
import java.util.List;

import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.ViewAnimator;

/**
 * @newfarking
 */
public class MyActivityGroup extends ActivityGroup {
    public MyActivityGroup group = null;
    /*
     * the getCurrentActivity method returns the last Activity
     * 
     * this field is used for get currently displayed activity
     */
    public List<String> currentActivityString = new ArrayList<String>();
    public ArrayList<View> history = null;
    /**
     * 页面切换的动画
     */
    public ViewAnimator viewAnimator = null;

    public final int DIALOG_APP_EXIT = 0;

    public void onCreate(Bundle savedInstanceState) {
        history = new ArrayList<View>();
        viewAnimator = new ViewAnimator(this);
        group = this;
        super.onCreate(savedInstanceState);
    }

    public void gotoActivity(LinearLayout viewGroup, Bundle bundle, Class<?> class1, String activityString) {
        Intent intent = new Intent(this, class1).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtras(bundle);
        Window w = getLocalActivityManager().startActivity(activityString, intent);

        currentActivityString.add(activityString);
        if (history.size() == 0) {
            viewAnimator.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.in_from_right));
        } else {
            viewAnimator.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.in_from_right));
            viewAnimator.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.out_to_left));
        }
        View view = w.getDecorView();
        history.add(view);
        viewAnimator.addView(view);
        viewAnimator.showNext();
        viewGroup.removeAllViews();

        LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        viewGroup.addView(viewAnimator);
    }

    public void gotoActivity(Bundle bundle, Class<?> class1, String activityString) {
        Intent intent = new Intent(this, class1).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtras(bundle);
        Window w = getLocalActivityManager().startActivity(activityString, intent);

        currentActivityString.add(activityString);
        if (history.size() == 0) {
            viewAnimator.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.in_from_right));
        } else {
            viewAnimator.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.in_from_right));
            viewAnimator.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.out_to_left));
        }
        View view = w.getDecorView();
        history.add(view);
        viewAnimator.addView(view);
        viewAnimator.showNext();
        group.setContentView(viewAnimator);
    }

    /**
     * 将事件传给子activity
     */
    @Override
    public void onBackPressed() {
        getLocalActivityManager().getActivity(currentActivityString.get(currentActivityString.size() - 1))
                        .onBackPressed();
    }

    public void onBackPressedAction() {
        if (history.size() > 1) {
            viewAnimator.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.in_from_left));
            viewAnimator.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.out_to_right));
            viewAnimator.showPrevious();
            viewAnimator.removeView(history.get(history.size() - 1));
            currentActivityString.remove(currentActivityString.size() - 1);
            history.remove(history.size() - 1);
            history.get(history.size() - 1).clearFocus();
            group.setContentView(viewAnimator);
        } else {
            showDialog(DIALOG_APP_EXIT);
        }
    }

    public void onBackPressedAction(ViewGroup viewGroup) {
        if (history.size() > 1) {
            viewAnimator.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.in_from_left));
            viewAnimator.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.out_to_right));
            viewAnimator.showPrevious();
            viewAnimator.removeView(history.get(history.size() - 1));
            currentActivityString.remove(currentActivityString.size() - 1);
            history.remove(history.size() - 1);
            history.get(history.size() - 1).clearFocus();
            viewGroup.removeAllViews();

            LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
            viewGroup.addView(viewAnimator, params);
        }
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        Dialog dialog = null;
        switch (id) {
        case DIALOG_APP_EXIT:
            dialog = new AlertDialog.Builder(this).setMessage("确定退出?").setTitle("提示")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                    android.os.Process.killProcess(android.os.Process.myPid());
                                    finish();
                                }
                            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).create();
        }
        return dialog;
    }
}
